﻿#region USING STATEMENTS

//using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using BrickBreaker.Core;

#endregion USING STATEMENTS

namespace BrickBreaker.Menu
{
    public class BBMenuSystem: BBIMenu
    {
        #region DATA MEMBERS
        //
            private List<BBText> mainMenuItems;
            private List<BBText> subMenu_GameMode;
            private List<BBText> subMenu_Help;
            //
            private int selectedIndex;
            //
            private BBBackground menuBG;
            private BBBackground helpBG;
            private BBBackground gameModeBG;
        //
        #endregion DATA MEMBERS


        #region OBJECT CONSTRUCTION
        //
            /// <summary>
            /// Creates a menu object.
            /// </summary>
            public BBMenuSystem()
        {
            mainMenuItems = new List<BBText>();
            subMenu_GameMode = new List<BBText>();
            subMenu_Help = new List<BBText>();

            Generate_MainMenu();
            //Sub-Menus...
            Generate_GameModeMenu();
            //Generate_OptionsMenu();
            Generate_HelpMenu();
            
            BBGlobalConfig.menuState = MenuState.MAIN;
            BBGlobalConfig.subMenuState = SubMenuState.RETURN;
           
            this.selectedIndex = 0;
        }
        //
        #endregion OBJECT CONSTRUCTION


        #region PRIVATE METHODS
        //
            /// <summary>
            /// Loads all the menu's content
            /// </summary>
            private void UpdateTextPositioning()
            {
                #region MENU

                // update the first texts X position the center it based on the center of the text
                mainMenuItems[0].Position -= new Vector2(mainMenuItems[0].Size.X / 2, 0);

                for (int i = 1; i < mainMenuItems.Count; i++)
                {
                    //if (i != 1 || i % 2 != 0)
                    //{
                    //    mainMenuItems[i].Position += new Vector2(mainMenuItems[i - 1].Position.X + mainMenuItems[i - 1].Size.X, mainMenuItems[i - 1].Position.Y + mainMenuItems[i - 1].Size.Y);
                    //}
                    //else
                    //{
                    // update each texts position by adding the previous texts X position and (Y position + height of text)
                    mainMenuItems[i].Position += new Vector2(mainMenuItems[i - 1].Position.X, mainMenuItems[i - 1].Position.Y + mainMenuItems[i - 1].Size.Y);
                    //}
                }

                #endregion MENU

                #region HELP
                
                subMenu_Help[0].Position -= new Vector2(subMenu_Help[0].Size.X / 2, 0);

                //for (int i = 1; i < subMenu_GameMode.Count; i++)
                //{
                    subMenu_Help[1].Position += new Vector2(subMenu_Help[0].Position.X /*- helpContents[i].Size.X / 2*/, subMenu_Help[0].Position.Y + subMenu_Help[0].Size.Y);
                //}
                //
                #endregion HELP

                #region GAME-MODES
                //
                subMenu_GameMode[0].Position -= new Vector2(subMenu_GameMode[0].Size.X / 2, 0);

                for (int i = 1; i < subMenu_GameMode.Count; i++)
                {
                    subMenu_GameMode[i].Position += new Vector2(subMenu_GameMode[i - 1].Position.X, subMenu_GameMode[i - 1].Position.Y + subMenu_GameMode[i - 1].Size.Y);
                }
                //
                #endregion GAME-MODES 
            }

            /// <summary>
            /// Generates the main menu's text.
            /// </summary>
            private void Generate_MainMenu()
            {
                mainMenuItems.Add(new BBText("New Game", new Vector2(BBGlobalConfig.getWindowWidth() / 2, BBGlobalConfig.getWindowHeight() / 3)));
                mainMenuItems.Add(new BBText("Continue", new Vector2(0, 5.0f)));
                mainMenuItems.Add(new BBText("Options", new Vector2(0, 5.0f)));
                mainMenuItems.Add(new BBText("Help", new Vector2(0, 5.0f)));
                mainMenuItems.Add(new BBText("Exit", new Vector2(0, 5.0f)));
            }

            /// <summary>
            /// Generates the game mode menu's text.
            /// </summary>
            private void Generate_GameModeMenu()
            {
                subMenu_GameMode.Add(new BBText("Classic", new Vector2(BBGlobalConfig.getWindowWidth() / 2 - 16, BBGlobalConfig.getWindowHeight() / 3)));
                subMenu_GameMode.Add(new BBText("Time Challenge", new Vector2(0, 5.0f)));
                subMenu_GameMode.Add(new BBText("Endless", new Vector2(0, 5.0f)));
                subMenu_GameMode.Add(new BBText("Player vs Computer", new Vector2(0, 5.0f)));
                subMenu_GameMode.Add(new BBText("Player vs Player", new Vector2(0, 5.0f)));
                subMenu_GameMode.Add(new BBText("Misc...", new Vector2(0, 5.0f)));
                subMenu_GameMode.Add(new BBText("Back", new Vector2(0, 5.0f)));
            }
            //
            #region NEEDS IMPLEMENTING
            //
                /// <summary>
                /// Generates the options menu's text.
                /// </summary>
                private void Generate_OptionsMenu()
            {
                //implement later...
            }
            //
            #endregion
            //
            /// <summary>
            /// Generates the help menu's text.
            /// </summary>
            private void Generate_HelpMenu()
            {
                string instructions = "Help \n\nMove Paddle (Left & Right Arrows) \n";

                subMenu_Help.Add(new BBText(instructions, new Vector2(BBGlobalConfig.getWindowWidth() / 4, BBGlobalConfig.getWindowHeight() / 3)));
                subMenu_Help.Add(new BBText("Main Menu (Backspace)", new Vector2(0, 5.0f)));
            }

            /// <summary>
            /// Updates which text in the menu is active
            /// </summary>
            private void ActivateMenuItem(List<BBText> list)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (i == selectedIndex)
                    {
                        if (!list[i].Active) { list[i].Active = true; }
                    }
                    else
                    {
                        if (list[i].Active) { list[i].Active = false; }
                    }
                    list[i].Update();
                }
            }

            /// <summary>
            /// Updates which text is currently selected.
            /// </summary>
            private void UpdateSelected_MenuItem(List<BBText> list)
            {
                if (BBGlobalConfig.keyboard.Down)
                {
                    if (selectedIndex < list.Count - 1)
                    {
                        selectedIndex++;
                    }
                    else { selectedIndex = 0; }
                }
                else if (BBGlobalConfig.keyboard.Up)
                {
                    if (selectedIndex > 0)
                    {
                        selectedIndex--;
                    }
                    else { selectedIndex = list.Count - 1; }
                }
            }
        //
        #endregion PRIVATE METHODS


        #region PUBLIC METHODS
        //
            /// <summary>
            /// Loads all the menu's content.
            /// </summary>
            public void LoadContent()
            {
                #region BACKGROUNDS

                menuBG = new BBBackground(BBGlobalConfig.content.Load<Texture2D>(BBGlobalConfig.BackgroundFolder + "background_menu"));
                helpBG = new BBBackground(BBGlobalConfig.content.Load<Texture2D>(BBGlobalConfig.BackgroundFolder + "background_help"));
                gameModeBG = new BBBackground(BBGlobalConfig.content.Load<Texture2D>(BBGlobalConfig.BackgroundFolder + "background_gamemode"));

                #endregion BACKGROUNDS

                UpdateTextPositioning();
            }

            /// <summary>
            /// Unloads all the menu's content, releasing memory back to the buffer.
            /// </summary>
            public void UnloadContent()
            {
                //implement later...
            }

            /// <summary>
            /// Updates the menu system, ensuring only the current menu is updated.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            public void Update(GameTime gameTime)
            {
                BBGlobalConfig.keyboard.Update();

                #region MENU UPDATE

                if (BBGlobalConfig.menuState == MenuState.MAIN)
                {
                    //Keyboard UP or DOWN called...
                    UpdateSelected_MenuItem(mainMenuItems);

                    //Keyboard ENTER called...
                    if (BBGlobalConfig.keyboard.Enter)
                    {
                        switch (selectedIndex)
                        {
                            case 0: //MenuState.NEW_GAME
                                BBGlobalConfig.menuState = MenuState.NEW_GAME;
                                break;
                            case 1:
                                BBGlobalConfig.menuState = MenuState.CONTINUE; //menuState = MenuState.Help;
                                break;
                            case 2: BBGlobalConfig.menuState = MenuState.OPTIONS;
                                break;
                            case 3: BBGlobalConfig.menuState = MenuState.HELP;
                                break;
                            case 4: BBGlobalConfig.menuState = MenuState.EXIT;
                                BBGlobalConfig.programState = ProgramState.EXIT;
                                break;
                            //case 2: BBScreenManager.isExiting = true;
                            //  break;
                        }//switch
                    }
                    //
                    ActivateMenuItem(mainMenuItems);
                } 
                #endregion MENU UPDATE


                #region GAME SELECTION UPDATE

                //GAME-MODE SELECTIOIN SUB-MENU
                else if (BBGlobalConfig.menuState == MenuState.NEW_GAME || BBGlobalConfig.menuState == MenuState.CONTINUE)
                {
                    //Keyboard UP or DOWN called...
                    UpdateSelected_MenuItem(subMenu_GameMode);

                    if (BBGlobalConfig.keyboard.Enter)
                    {
                        //beforeSubMenuState = SubMenuState.

                        //switch to selected GameMode
                        switch (selectedIndex)//((int)BBGlobalConfig.subMenuState)
                        {
                            case 0: //SubMenuState.CLASSIC:
                                BBGlobalConfig.subMenuState = SubMenuState.CLASSIC;
                                BBGlobalConfig.programState = ProgramState.PLAY;
                                break;
                            case 1: //SubMenuState.TIMED:
                                BBGlobalConfig.subMenuState = SubMenuState.TIMED;
                                BBGlobalConfig.programState = ProgramState.PLAY;
                                break;
                            case 2: //SubMenuState.ENDLESS:
                                BBGlobalConfig.subMenuState = SubMenuState.ENDLESS;
                                BBGlobalConfig.programState = ProgramState.PLAY;
                                break;
                            case 3: //SubMenuState.PvP:
                                BBGlobalConfig.subMenuState = SubMenuState.PvP;
                                BBGlobalConfig.programState = ProgramState.PLAY;
                                break;
                            case 4: //SubMenuState.PvC:
                                BBGlobalConfig.subMenuState = SubMenuState.PvC;
                                BBGlobalConfig.programState = ProgramState.PLAY;
                                break;
                            case 5: //SubMenuState.MISC:
                                BBGlobalConfig.subMenuState = SubMenuState.MISC;
                                BBGlobalConfig.programState = ProgramState.PLAY;
                                break;
                            case 6: //SubMenuState.RETURN:
                                BBGlobalConfig.subMenuState = SubMenuState.RETURN;
                                BBGlobalConfig.programState = ProgramState.MENU;
                                BBGlobalConfig.menuState = MenuState.MAIN;
                                selectedIndex = 0;
                                break;
                        }
                    }
                    //
                    ActivateMenuItem(subMenu_GameMode);
                }

                #endregion GAME SELECTION UPDATE


                #region HELP UPDATE

                //HELP SUB-MENU
                else if (BBGlobalConfig.menuState == MenuState.HELP || BBGlobalConfig.menuState == MenuState.OPTIONS)
                {
                    if (BBGlobalConfig.keyboard.Enter || BBGlobalConfig.keyboard.BackSpace ||
                        BBGlobalConfig.keyboard.Escape || BBGlobalConfig.keyboard.Left)
                    {
                        BBGlobalConfig.menuState = MenuState.MAIN; //MenuState.Menu;
                    }
                    if (!subMenu_Help[1].Active)
                    {
                        subMenu_Help[1].Active = true;
                        subMenu_Help[1].Update();
                    }
                }
                //EXIT: PROMPT SAVE Options B4 QUITTING
                else if (BBGlobalConfig.menuState == MenuState.EXIT)
                {
                    //FUTURE CALL TO PROVIDE SAVE MECHNISM...
                }

                #endregion HELP UPDATE

            }

            /// <summary>
            /// Draws the current menu only.
            /// </summary>
            /// <param name="spriteBatch">Your spritebatch.  Make sure you have already called begin().</param>
            public void Draw(SpriteBatch spriteBatch)
        {
            //implement later...
            #region DRAW MAIN-MENU
            if (BBGlobalConfig.menuState == MenuState.MAIN)
            {
                menuBG.Draw(spriteBatch);

                foreach (BBText t in mainMenuItems)
                {
                    if (t.Active) {
                        //spriteBatch.DrawString(BBGlobalConfig.fontSelected, t.Position.X + "  " + t.Position.Y, new Vector2(t.Position.X - 100, t.Position.Y), Color.Blue);
                        t.Draw(spriteBatch, BBGlobalConfig.fontSelected);
                    }
                    else {
                        //spriteBatch.DrawString(BBGlobalConfig.font, t.Position.X + "  " + t.Position.Y, new Vector2(t.Position.X - 100, t.Position.Y), Color.Blue);
                        t.Draw(spriteBatch, BBGlobalConfig.font);
                    }
                }
            }
            //
            #endregion DRAW MAIN-MENU


            #region  DRAW GAME_MODE SUB-MENU
            //
            else if (BBGlobalConfig.menuState == MenuState.NEW_GAME || BBGlobalConfig.menuState == MenuState.CONTINUE)
            {
                gameModeBG.Draw(spriteBatch);

                foreach (BBText t in subMenu_GameMode)
                {
                    if (t.Active)
                    {
                        //spriteBatch.DrawString(BBGlobalConfig.fontSelected, t.Position.X + "  " + t.Position.Y, new Vector2(t.Position.X - 100, t.Position.Y), Color.Blue);
                        t.Draw(spriteBatch, BBGlobalConfig.fontSelected);
                    }
                    else {
                       // spriteBatch.DrawString(BBGlobalConfig.font, t.Position.X + "  " + t.Position.Y, new Vector2(t.Position.X - 100, t.Position.Y), Color.Blue);
                        t.Draw(spriteBatch, BBGlobalConfig.font);
                    }
                }
            }
            //
            #endregion DRAW GAME_MODE SUB-MENU


            #region  DRAW HELP
            //
            else if (BBGlobalConfig.menuState == MenuState.HELP || BBGlobalConfig.menuState == MenuState.OPTIONS)
            {
                helpBG.Draw(spriteBatch);

                foreach (BBText t in subMenu_Help)
                {
                    t.Draw(spriteBatch, BBGlobalConfig.font);
                }
            }
            //
            #endregion DRAW HELP
        }
        //
        #endregion PUBLIC METHODS

    }//end class
}//end namespace
  
